﻿#include <osg/Geode>
#include <osg/Point>
#include <osg/LineWidth>
#include <osg/TexGen>
#include <osg/TexEnv>
#include <CommonLib/Common/Common.h>
#include <Solar/Line.h>
CLine::CLine()
	:m_rpDrawArrays(nullptr)
	,m_rpVertexArray(nullptr)
	,m_rpGeo(nullptr)
{
	osg::ref_ptr<osg::Geode> rpGeode = new osg::Geode;
	rpGeode->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF | osg::StateAttribute::OVERRIDE);
	rpGeode->getOrCreateStateSet()->setMode(GL_BLEND,osg::StateAttribute::ON);
	rpGeode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
	//rpGeode->getOrCreateStateSet()->setRenderBinDetails(11,"DepthSortedBin");
	this->addChild(rpGeode.get());
	{
		m_rpGeo = new osg::Geometry;
		m_rpGeo->setUseDisplayList(false);
		m_rpGeo->setUseVertexBufferObjects(true);

		m_rpVertexArray = new osg::Vec3Array;
		m_rpGeo->setVertexArray(m_rpVertexArray.get());

		osg::ref_ptr<osg::Vec4Array> rpColorArray = new osg::Vec4Array;
		rpColorArray->push_back(osg::Vec4(1.0,1.0,1.0,1.0));
		m_rpGeo->setColorArray(rpColorArray.get(),osg::Array::BIND_OVERALL);

		m_rpDrawArrays = new osg::DrawArrays();
		m_rpGeo->addPrimitiveSet(m_rpDrawArrays.get());//设置顶点连接方式

		osg::ref_ptr<osg::Point> rpPoint = new osg::Point(1);
		m_rpGeo->getOrCreateStateSet()->setAttribute(rpPoint.get(),osg::StateAttribute::ON);
		osg::ref_ptr<osg::LineWidth> rpLineWidth = new osg::LineWidth(1);
		m_rpGeo->getOrCreateStateSet()->setAttribute(rpLineWidth.get(),osg::StateAttribute::ON);

		rpGeode->addDrawable(m_rpGeo.get());
		//setPrimitive(osg::PrimitiveSet::POINTS);
	}
}

CLine::~CLine()
{

}

void CLine::setPointSize( int nSize )
{
	osg::ref_ptr<osg::Point> rpPoint = dynamic_cast<osg::Point*>(m_rpGeo->getOrCreateStateSet()->getAttribute(osg::StateAttribute::POINT));
	if (rpPoint.valid())
	{
		rpPoint->setSize(nSize);
	}
}

void CLine::setLineWidth( int nWidth )
{
	osg::ref_ptr<osg::LineWidth> rpLineWidth = dynamic_cast<osg::LineWidth*>(m_rpGeo->getOrCreateStateSet()->getAttribute(osg::StateAttribute::LINEWIDTH));
	if (rpLineWidth.valid())
	{
		rpLineWidth->setWidth(nWidth);
	}
}

void CLine::setColor( const osg::Vec4& vecColor )
{
	osg::ref_ptr<osg::Vec4Array> rpColorArray = dynamic_cast<osg::Vec4Array*>(m_rpGeo->getColorArray());
	if (rpColorArray.valid())
	{
		rpColorArray->clear();
		rpColorArray->push_back(vecColor);
		rpColorArray->dirty();
	}
}
